package com.sw.small_world.fragment.me.mycircle;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.ClickButtonActivity;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.StartUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 */

public class MyCirclesFragment extends BaseFragment {

    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.add_circle)
    TextView addCircle;
    private ClickButtonActivity activity;
    ArrayList<String> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ClickButtonActivity) getActivity();
        setActionBar();
    }

    @Override
    protected Object requestData() {
        list.add("");
        list.add("");
        list.add("");
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View view = View.inflate(activity, R.layout.fragment_my_circles, null);
        ButterKnife.bind(this, view);
        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StartUtils.startActivityById(activity, ConstantUtils.MY_CIRCLE);
            }
        });
        addCircle.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_circle:
                StartUtils.startActivityByIdForResult(this,v.getId(),ConstantUtils.CODE_ADD_CIRCLES);
                break;
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_my_circles, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


    }

    private void setActionBar() {
        activity.tvTitle.setText("我的圈子");
    }

    static class ViewHolder {
        @Bind(R.id.circle_name)
        TextView circleName;
        @Bind(R.id.circle_person)
        TextView circlePerson;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
